﻿using System.Web.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Collections.Specialized;

namespace Marbles.Core.Web.SiteMapManager
{
    public abstract class SiteMapBase
    {

        public SiteMapNode Nodes
        {
            get { return this._nodes; }
            set { this._nodes = value; }
        }

        internal SiteMapNode _nodes = null;
        internal SiteMapProvider prov = null;
        public SiteMapBase()
        {
            SiteMapSection cfg = WebConfigurationManager.GetSection("system.web/siteMap") as SiteMapSection;
            NameValueCollection providerAttributes = new NameValueCollection(1);
            providerAttributes.Add("siteMapFile", "Web.sitemap");
            prov = new XmlSiteMapProvider();
            prov.Initialize(cfg.DefaultProvider, providerAttributes);
        }

        public SiteMapNode CreateSiteMapNode(string key, string url, string title, string description)
        {
            SiteMapNode node = new SiteMapNode(this.prov, key);
            node.Url = url;
            node.Title = title;
            node.Description = description;
            return node;
        }

        public void AddSiteMapNode(SiteMapNode parent, string key, string url, string title, string description)
        {
            if (parent == null)
            {
                parent = CreateSiteMapNode(key, url, title, description);
            }
            else
            {
                parent.ChildNodes.Add(CreateSiteMapNode(key, url, title, description));
            }
        }

        public void AddSiteMapNode(string parentkey, string key, string url, string title, string description)
        {
            if ((this._nodes == null) || (parentkey == string.Empty))
            {
                if (this._nodes == null)
                {
                    this._nodes = CreateSiteMapNode(key, url, title, description);
                }
                else
                {
                    this._nodes.ChildNodes.Add(CreateSiteMapNode(key, url, title, description));
                }
                return;
            }

            SiteMapNode node = this.FindSiteMapNode(parentkey);
            if (node != null)
            {
                node.ChildNodes.Add(CreateSiteMapNode(key, url, title, description));
            }
        }

        public SiteMapNode FindSiteMapNode(string key)
        {
            return FindSiteMapNode(this._nodes, key);
        }

        public SiteMapNode FindSiteMapNode(SiteMapNode parent, string key)
        {
            SiteMapNode node = null;
            if (parent.Key == key) { return node; }

            foreach (SiteMapNode child in parent.ChildNodes)
            {
                if (child.Key == key) { return child; }
                if (child.HasChildNodes)
                {
                    node = FindSiteMapNode(child, key);
                    if (node != null) { return node; }
                }
            }
            return node;
        }

    }
}
